﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using _7DaysToDieTools.Class;
using System.Diagnostics;
using ICSharpCode.SharpZipLib;

namespace _7DaysToDieTools
{
    public partial class FormMain : Form
    {
        #region 初始化
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Sinfo.Check();
            switch (Sinfo.gameType)
            {
                case Sinfo.GameType.Client:
                    labelGameTypeV.Text = "客户端";
                    break;
                case Sinfo.GameType.Server:
                    labelGameTypeV.Text = "服务端";
                    break;
            }
            labelGamePathV.Text = Sinfo.PathCurr;
            this.Text = "七日杀工具集V" + Application.ProductVersion.ToString();

        }

        private void FormMain_Shown(object sender, EventArgs e)
        {
            if (!BackAction.CheckHaveBack())
            {
                MessageBox.Show("检测到没有进行数据备份！不备份的话，出错了有时候会进不了游戏，请先进行备份！");
            }
            //是否创建DATA文件夹
            if (!Directory.Exists(Sinfo.Data.BasePath)) Directory.CreateDirectory(Sinfo.Data.BasePath);
            //初始化一键设置列表
            LangAction.LoadLangList(cboxOKLang);
            BuildAction.LoadBuildList(cboxOKBuild);
            StackAction.LoadStackList(cboxOKStack);
        }
        #endregion

        #region 方法集

        #endregion

        #region  游戏信息
        /// <summary>
        /// 开始游戏
        /// </summary>
        private void buttStart_Click(object sender, EventArgs e)
        {
            try
            {
                Process P = new Process();
                switch (Sinfo.gameType)
                {
                    case Sinfo.GameType.Client:
                        P.StartInfo.FileName = Sinfo.PathCurr + Sinfo.File.GameName;
                        break;
                    case Sinfo.GameType.Server:
                        P.StartInfo.FileName = Sinfo.PathCurr + "startdedicated.bat";
                        break;
                }
                P.StartInfo.WorkingDirectory = Sinfo.PathCurr;
                if (!P.Start())
                {
                    MessageBox.Show("启动出错：\n" + P.StandardError.ToString());
                }
            }
            catch (Exception x)
            {
                MessageBox.Show("启动出错：\n" + x.Message);
            }
        }

        /// <summary>
        /// 打开游戏目录
        /// </summary>
        private void buttGamePath_Click(object sender, EventArgs e)
        {
            Process.Start(Sinfo.PathCurr);
        }

        private void llabelEACWeb_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            llabelEACWeb.Links[0].LinkData = @"http://git.oschina.net/alwcel/AC_7DaysToDieTools/wikis/Stream-%E6%97%A0EAC%E5%90%AF%E5%8A%A8%E6%96%B9%E6%B3%95";
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }
        #endregion

        #region 工具列表
        private void buttToolLang_Click(object sender, EventArgs e)
        {
            new FormLang(this).ShowDialog();
        }

        private void buttToolStack_Click(object sender, EventArgs e)
        {
            new FormStack(this).ShowDialog();
        }

        private void buttToolSerConf_Click(object sender, EventArgs e)
        {
            new FormSconfig(this).ShowDialog();
        }

        private void buttBagDll_Click(object sender, EventArgs e)
        {
            new FormBagDll(this).ShowDialog();
        }

        private void buttServerManage_Click(object sender, EventArgs e)
        {
            this.Hide();
            new FormServer().ShowDialog();
            this.Show();
        }

        private void buttModManage_Click(object sender, EventArgs e)
        {
            new FormMods(this).ShowDialog();
        }
        #endregion

        #region 一键设置
        private void cboxOKLang_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                cboxOKLang.SelectedIndex = -1;
            }
        }

        private void cboxOKBag_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                cboxOKBag.SelectedIndex = -1;
            }
        }

        private void cboxOKBuild_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                cboxOKBuild.SelectedIndex = -1;
            }
        }

        private void cboxOKStack_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                cboxOKStack.SelectedIndex = -1;
            }
        }

        /// <summary>
        /// 一键设置
        /// </summary>
        private void buttOneKey_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //--汉化----------
                switch (cboxOKLang.SelectedIndex)
                {
                    case 0:
                        LangAction.ApplyLangLocal();
                        break;
                    case 1:
                        LangAction.ApplyLangNet();
                        break;
                    default:
                        break;
                }
                //--大背包----------
                switch (cboxOKBag.SelectedIndex)
                {
                    case 0:
                        BagAction.SetBigBackpack56();
                        break;
                    case 1:
                        BagAction.SetBigBackpack112();
                        break;
                    default:
                        break;
                }
                //--建筑----------
                switch (cboxOKBuild.SelectedIndex)
                {
                    case -1:
                        break;
                    case 0:
                        BuildAction.SetBuildDefault();
                        break;
                    case 1:
                        BuildAction.SetLocalBuildMore();
                        break;
                    default:
                        string buildName = cboxOKBuild.SelectedItem.ToString().Remove(0, 4);
                        BuildAction.SetBuildNet(buildName);
                        break;
                }
                //--叠加----------
                switch (cboxOKStack.SelectedIndex)
                {
                    case -1:
                        break;
                    case 0:
                        StackAction.GetStackDefault();
                        StackAction.SetStack();
                        break;
                    case 1:
                        StackAction.GetStackMore();
                        StackAction.SetStack();
                        break;
                    default:
                        string stackName = cboxOKStack.SelectedItem.ToString().Remove(0, 4);
                        StackAction.GetStackNet(stackName);
                        StackAction.SetStack();
                        break;
                }
                this.Cursor = Cursors.Default;
                MessageBox.Show("设置成功！");
            }
            catch(Exception x)
            {
                MessageBox.Show("设置出错：\n" + x.Message);
            }
        }

        #endregion

        #region 备份还原
        private void buttBackAll_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("是否备份所有文件？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }
                if (BackAction.CheckHaveBack())
                {
                    if (MessageBox.Show("已经有备份，是否覆盖？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }
                }

                BackAction.BackAll();
                MessageBox.Show("备份完成！");
            }
            catch(Exception x)
            {
                MessageBox.Show("备份失败！\n" + x.Message);
            }
        }

        private void buttRestore_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("是否还原所有更改？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return;
                }

                BackAction.RestoreALL();
                MessageBox.Show("还原完成！如果还是正正常。请使用MOD管理里面的原版MOD，或者用重新下载游戏");
            }
            catch(Exception x)
            {
                MessageBox.Show("还原出错：\n" + x.Message);
            }
        }
        #endregion
    }
}
